package com.qiantu.phone.ui.adapter;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;

public class SceneIntentListAdapter extends BaseQuickAdapter<SceneBean, BaseViewHolder> {

    private int itemWidth;
    private int dp12;

    public SceneIntentListAdapter(int itemWidth) {
        super(R.layout.item_scene_intent);
        this.itemWidth = itemWidth;
        dp12 = AppApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.dp_12);
    }

    @Override
    protected void convert(BaseViewHolder helper, SceneBean item) {
        ImageView icon = helper.getView(R.id.icon);
        TextView name = helper.getView(R.id.name);
        name.setText(item.getName());
        String url = BuildConfig.APP_SCENE_BASE + ThemeUtil.getIconThemeSuffix() + item.getImage() + ".png";
        GlideApp.with(getContext())
                .load(url)
                //.placeholder(R.drawable.avatar_placeholder_ic)
                //.error(R.drawable.avatar_placeholder_ic)
                .into(icon);
        ViewGroup.MarginLayoutParams gl = (ViewGroup.MarginLayoutParams) helper.itemView.getLayoutParams();
        gl.width = itemWidth;
        gl.height = itemWidth;
        int position = helper.getLayoutPosition();
        if (position == 0 || position == 1 || position == 2){
            gl.leftMargin = 0;
            gl.rightMargin = 0;
            gl.topMargin = dp12;
            gl.bottomMargin = 0;
        } else if(((position == getData().size() - 1 || position == getData().size() - 2 || position == getData().size() - 3) && getData().size() % 3 == 0)
                || ((position == getData().size() - 1) && getData().size() % 3 == 1)
                || ((position == getData().size() - 1 || position == getData().size() - 2) && getData().size() % 3 == 2)){
            gl.leftMargin = 0;
            gl.rightMargin = 0;
            gl.topMargin = 0;
            gl.bottomMargin = dp12;
        }else{
            gl.leftMargin = 0;
            gl.rightMargin = 0;
            gl.topMargin = 0;
            gl.bottomMargin = 0;
        }
    }
}
